package com.xqboss.apps.service.nft;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.nft.NftOld;
import com.xqboss.apps.domain.nft.PrizeGift;
import com.xqboss.apps.mapper.nft.PrizeGiftMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HaoLun
 * @since 2023-01-10
 */
@Service
public class PrizeGiftService extends ServiceImpl<PrizeGiftMapper, PrizeGift> {

    @Autowired
    private NftOldService nftOldService;

    public void addGift(Long relateId, List<PrizeGift> prizeGifts, Integer prizeType) {
        this.remove(new LambdaQueryWrapper<PrizeGift>().eq(PrizeGift::getRelateId, relateId).eq(PrizeGift::getRelateType, prizeType));
        if (prizeGifts == null || prizeGifts.isEmpty()) {
            return;
        }
        prizeGifts.forEach(prizeGift -> {
            prizeGift.setPrizeType(PrizeGift.PrizeTypeEnum.NFT.getValue());
            prizeGift.setRelateId(relateId);
            prizeGift.setRelateType(PrizeGift.PrizeTypeEnum.NFT.getValue());
        });
        this.saveBatch(prizeGifts);
    }

    /**
     * 添加礼包
     * 礼包内容积分
     *
     * @param relateId   礼包藏品的关联id
     * @param prizeGifts 奖品列表
     */
    public void addGift(Long relateId, List<PrizeGift> prizeGifts) {
        // 删除之前道具藏品对应的玉珠天珠灵珠礼包内容
        this.remove(new LambdaQueryWrapper<PrizeGift>()
                .eq(PrizeGift::getRelateId, relateId)
                .in(PrizeGift::getRelateType, 2, 3, 4, 5, 6, 7, 8, 9)
                .in(PrizeGift::getPrizeType, 2, 3, 4, 5, 6, 7, 8, 9));
        if (prizeGifts == null || prizeGifts.isEmpty()) {
            return;
        }
        // 封装礼包
        ArrayList<PrizeGift> prizeGiftsNew = new ArrayList<>();
        // 遍历礼包内容
        for (PrizeGift prizeGift : prizeGifts) {
            if (prizeGift.getIsShow() != null && !prizeGift.getIsShow()) {  // 是否传了这个值
                continue;
            }
            prizeGift.setPrizeType(prizeGift.getIntegralType());    // 奖品类型
            prizeGift.setRelateType(prizeGift.getIntegralType());   // 关联ID类型
            prizeGift.setRelateId(relateId);    // 关联ID
//            prizeGift.setGrantNum(prizeGift.getGrantNum());
            prizeGift.setPrizeId(1L);   // 奖品ID
            prizeGiftsNew.add(prizeGift);   // 添加到列表
        }
        this.saveBatch(prizeGiftsNew);  // 批量添加
    }

//    /**
//     * 礼包内容积分-修改
//     * @param prizeGifts
//     */
//    public void updateGift(Long relateId, List<PrizeGift> prizeGifts){
//        prizeGifts.forEach(prizeGift -> {
//            prizeGift.setPrizeType(PrizeGift.PrizeTypeEnum.INTEGRAL.getValue());
//            prizeGift.setRelateId(relateId);
//            prizeGift.setGrantNum(prizeGift.getGrantNum());
//            prizeGift.setPrizeId(1L);
//            prizeGift.setRelateType(PrizeGift.PrizeTypeEnum.INTEGRAL.getValue());
//        });
//        this.saveBatch(prizeGifts);
//    }


    public List<PrizeGift> prizeGiftList(Long relateId, Integer prizeType) {
        List<PrizeGift> prizeGifts = this.list(new LambdaQueryWrapper<PrizeGift>().eq(PrizeGift::getRelateId, relateId).eq(PrizeGift::getRelateType, prizeType));
        prizeGifts.forEach(prizeGift -> {
            NftOld nftOld = nftOldService.getById(prizeGift.getPrizeId());
            prizeGift.setPrizeImg(nftOld.getMainImg());
            prizeGift.setPrizeTitle(nftOld.getTitle());
            prizeGift.setPrizeExtraStock(nftOld.getExtraStock());
        });

        return prizeGifts;

    }

    /**
     * 获取积分
     *
     * @return
     */
    public List<PrizeGift> getIntegral(Long nftId) {
        List<PrizeGift> prizeGifts = this.list(new LambdaQueryWrapper<PrizeGift>().eq(PrizeGift::getRelateId, nftId)
                .in(PrizeGift::getPrizeType, PrizeGift.PrizeTypeEnum.INTEGRAL.getValue(),
                        PrizeGift.PrizeTypeEnum.jindou.getValue(),
                        PrizeGift.PrizeTypeEnum.energy.getValue(),
                        PrizeGift.PrizeTypeEnum.METALS.getValue(),
                        PrizeGift.PrizeTypeEnum.TREE.getValue(),
                        PrizeGift.PrizeTypeEnum.WATER.getValue(),
                        PrizeGift.PrizeTypeEnum.FIRE.getValue(),
                        PrizeGift.PrizeTypeEnum.SOIL.getValue()
                )
        );
//        List<IntegralVO> integralVOs = new LinkedList<>();
        List<PrizeGift> prizeGiftNew = new ArrayList<>();
        //构建固定的三条数据
        for (int i = 0; i < 8; i++) {
            PrizeGift prizeGift = new PrizeGift();
            switch (i) {
                case 0:
                    prizeGift.setIsShow(false);
                    prizeGift.setIntegralType(PrizeGift.PrizeTypeEnum.INTEGRAL.getValue());
                    break;
                case 1:
                    prizeGift.setIsShow(false);
                    prizeGift.setIntegralType(PrizeGift.PrizeTypeEnum.jindou.getValue());
                    break;
                case 2:
                    prizeGift.setIsShow(false);
                    prizeGift.setIntegralType(PrizeGift.PrizeTypeEnum.energy.getValue());
                    break;
                case 3:
                    prizeGift.setIsShow(false);
                    prizeGift.setIntegralType(PrizeGift.PrizeTypeEnum.METALS.getValue());
                    break;
                case 4:
                    prizeGift.setIsShow(false);
                    prizeGift.setIntegralType(PrizeGift.PrizeTypeEnum.TREE.getValue());
                    break;
                case 5:
                    prizeGift.setIsShow(false);
                    prizeGift.setIntegralType(PrizeGift.PrizeTypeEnum.WATER.getValue());
                    break;
                case 6:
                    prizeGift.setIsShow(false);
                    prizeGift.setIntegralType(PrizeGift.PrizeTypeEnum.FIRE.getValue());
                    break;
                case 7:
                    prizeGift.setIsShow(false);
                    prizeGift.setIntegralType(PrizeGift.PrizeTypeEnum.SOIL.getValue());
                    break;
            }
            prizeGiftNew.add(prizeGift);
        }
        List<Integer> list = prizeGifts.stream().map(PrizeGift::getPrizeType).collect(Collectors.toList());
        Map<Integer, Integer> integerMap = prizeGifts.stream().collect(Collectors.toMap(PrizeGift::getPrizeType, PrizeGift::getGrantNum));
        prizeGiftNew.forEach(prizeGift -> {
            switch (prizeGift.getIntegralType()) {
                case 2:
                    if (list.contains(PrizeGift.PrizeTypeEnum.INTEGRAL.getValue())) {
                        prizeGift.setIsShow(true);
                        prizeGift.setGrantNum(integerMap.get(2));
                    }
                    break;
                case 3:
                    if (list.contains(PrizeGift.PrizeTypeEnum.jindou.getValue())) {
                        prizeGift.setIsShow(true);
                        prizeGift.setGrantNum(integerMap.get(3));
                    }
                    break;
                case 4:
                    if (list.contains(PrizeGift.PrizeTypeEnum.energy.getValue())) {
                        prizeGift.setIsShow(true);
                        prizeGift.setGrantNum(integerMap.get(4));
                    }
                    break;
                case 5:
                    if (list.contains(PrizeGift.PrizeTypeEnum.METALS.getValue())) {
                        prizeGift.setIsShow(true);
                        prizeGift.setGrantNum(integerMap.get(5));
                    }
                    break;
                case 6:
                    if (list.contains(PrizeGift.PrizeTypeEnum.TREE.getValue())) {
                        prizeGift.setIsShow(true);
                        prizeGift.setGrantNum(integerMap.get(6));
                    }
                    break;
                case 7:
                    if (list.contains(PrizeGift.PrizeTypeEnum.WATER.getValue())) {
                        prizeGift.setIsShow(true);
                        prizeGift.setGrantNum(integerMap.get(7));
                    }
                    break;
                case 8:
                    if (list.contains(PrizeGift.PrizeTypeEnum.FIRE.getValue())) {
                        prizeGift.setIsShow(true);
                        prizeGift.setGrantNum(integerMap.get(8));
                    }
                    break;
                case 9:
                    if (list.contains(PrizeGift.PrizeTypeEnum.SOIL.getValue())) {
                        prizeGift.setIsShow(true);
                        prizeGift.setGrantNum(integerMap.get(9));
                    }
                    break;
            }
        });
        return prizeGiftNew;
    }
}
